/**
 * Created by apple on 16/3/31.
 */

class Controller {
    constructor (systemService, uiNotification, statusMap, item, type, plat, $modalInstance) {
        "ngInject"
        this.uiModal       = $modalInstance;
        this.service       = systemService;
        this.notify        = uiNotification;
        this.isEdit        = true;
        this.select        = {
            plats: plat.data,
            status: statusMap
        }
        this.data          = angular.copy(item);
        this.data.statusCd = "S0A";
        this.data.initFlag = "1";
        this.type          = type;

        if (type == 'user') {
            systemService.getPwdRule('select').then(res => {
                __DEBUG__ && console.log(res)
                this.select.strategy = res.data;
            })
        }
    }

    save () {
        if ( this.submitting ) return;

        this.submitting = true;

        let param = {
            orgId: this.data.orgId
        };

        switch (this.type) {
            case "post" :
                angular.extend(param, {
                    sysPostId  : this.data.sysPostId,
                    sysPostCode: this.data.sysPostCode,
                    sysPostName: this.data.sysPostName,
                    sysPostDesc: this.data.sysPostDesc,
                    statusCd   : this.data.statusCd
                })
                break;
            case "staff" :
                angular.extend(param, {
                    staffId    : this.data.staffId,
                    staffDesc  : this.data.staffDesc,
                    staffCode  : this.data.staffCode,
                    statusCd   : this.data.statusCd,
                    staffName  : this.data.staffName,
                    mobilePhone: this.data.mobilePhone
                })
                break;
            case "user" :
                angular.extend(param, {
                    staffId    : this.data.staffId,
                    sysUserId  : this.data.sysUserId,
                    sysUserCode: this.data.sysUserCode,
                    password   : this.data.password,
                    statusCd   : this.data.statusCd,
                    sysUserDesc: this.data.sysUserDesc,
                    passRuleId : this.data.passRuleId,
                    passNeedModify: this.data.passNeedModify
                })
                break;
        }

        this.service.update(this.type, param).then(() => {
            if (this.type == 'user') {
                let item = _.find(this.select.plats, {platSysNbr: this.data.sysCode});
                this.data.platInfo = {
                    platName : item && item['platName']
                }
            }
            this.uiModal.close({
                type: this.type,
                item: this.data
            });

            this.notify.success({
                'title'  : '提示',
                'message': '操作成功!'
            });

        }).catch((data) => {

            this.notify.error({
                'title'  : '提示',
                'message': '操作失败. ' + (data.resultMsg ? data.resultMsg : '')
            });

        }).finally(() => {
            // 隐藏 loading
            this.submitting = false;
        });
    }

    checkValid (field) {
        if ( field.$pristine ) {
            field.$pristine = false;
        }
    }

    generate () {
        if ( this.making ) return;
        this.making = true;
        this.service
            .generate('pwd')
            .then((res) => {
                this.data.password = res.data;
            }).catch((data) => {
            this.notify.error({
                'title'  : '提示',
                'message': '操作失败. ' + (data.resultMsg ? data.resultMsg : '')
            });
        }).finally(() => {
            // 隐藏 loading
            this.making = false;
        })
    }

    getPwdRule() {
        this.service.getPwdRule().then((res) => {
            console.log(res)
        })
    }

    cancel () {
        this.uiModal.dismiss('cancel')
    }
}

export default Controller;